package com.sinoglobal.ningxia.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ty
 * @createdate 2012-6-14 下午6:09:16
 * @Description: 时间工具类
 */
public class TimeUtil {
    public static final DateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    // public static final DateFormat sdf2 = new
    // SimpleDateFormat("yyyy年MM月dd日");
    // public static final DateFormat sdf3=new
    // SimpleDateFormat("yyyy年MM月dd日 E"); //E表示星期几
    // public static final DateFormat sdf4=new
    // SimpleDateFormat("yyyy-MM-dd HH"); //精确到小时
    // private static final DateFormat dfYear = new SimpleDateFormat("yyyy");
    // private static final DateFormat dfDay = new SimpleDateFormat("dd");
    // private static final DateFormat dfMonth = new
    // SimpleDateFormat("MM-dd HH:mm");
    public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static final DateFormat dfHour = new SimpleDateFormat("HH:mm");
    private static final String SUNDAY = "周日";
    private static final String SUNDAYS = "星期日";
    private static final String MONDAY = "周一";
    private static final String MONDAYS = "星期一";
    private static final String TUESDAY = "周二";
    private static final String TUESDAYS = "星期二";
    private static final String WEDNESDAY = "周三";
    private static final String WEDNESDAYS = "星期三";
    private static final String THURSDAY = "周四";
    private static final String THURSDAYS = "星期四";
    private static final String FRIDAY = "周五";
    private static final String FRIDAYS = "星期五";
    private static final String SATURDAY = "周六";
    private static final String SATURDAYS = "星期六";

    /**
     * @param df
     * @param date
     * @return
     * @author ty
     * @createdate 2012-6-17 下午1:53:04
     * @Description: 将字符串根据日期的格式进行格式化
     */
    public static Date parseDate(DateFormat df, String date) {
        try {
            return df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param lo 毫秒数
     * @return String yyyy-MM-dd HH:mm:ss
     * @Description: String类型毫秒数转换成日期
     */
    public static String stringToDate(String lo) {
        try {
            long time = Long.parseLong(lo);
            Date date = new Date(time);
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sd.format(date);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param lo 日期毫秒数
     * @return String yyyy.MM.dd
     * @Description: long类型转换成点形式的日期格式
     */
    public static String getLongPointDate(long lo) {
        try {
            Date date = new Date(lo);
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return sd.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * @param df
     * @param date
     * @return
     * @author ty
     * @createdate 2012-9-23 下午3:24:34
     * @Description: 将Long值格式化
     */
    public static String parseDateToString(DateFormat df, Date date) {
        return df.format(date);
    }

    public static String parseDateToString(Date date) {
        return df.format(date);
    }

    /**
     * @param calendar
     * @return
     * @author ty
     * @createdate 2012-9-22 下午4:40:30
     * @Description: 指定的日期返回日期(yyyy-MM-dd)、年、月、日、星期几
     */
    public static MCalendar getDateStyle(Calendar calendar) {
        Date date = calendar.getTime();
        String datestr = parseDateToString(sdf1, date);
        String[] da = datestr.split(MContants.DATE_SUB);
        String[] weeks = getWeek(calendar.get(Calendar.DAY_OF_WEEK) - 1);
        MCalendar cal = new MCalendar();
        cal.setYear(da[0]);
        cal.setMonth(da[1]);
        cal.setDay(da[2]);
        cal.setWeek(weeks[0]);
        cal.setWeekStr(weeks[1]);
        cal.setDate(datestr);
        weeks = null;
        da = null;
        return cal;
    }

    /**
     * @param weekNum
     * @return
     * @author ty
     * @createdate 2012-9-22 下午4:47:08
     * @Description: 获取星期几
     */
    public static String[] getWeek(int weekNum) {
        String[] week = null;
        switch (weekNum) {
            case 0:
                week = new String[]{SUNDAY, SUNDAYS};
                break;
            case 1:
                week = new String[]{MONDAY, MONDAYS};
                break;
            case 2:
                week = new String[]{TUESDAY, TUESDAYS};
                break;
            case 3:
                week = new String[]{WEDNESDAY, WEDNESDAYS};
                break;
            case 4:
                week = new String[]{THURSDAY, THURSDAYS};
                break;
            case 5:
                week = new String[]{FRIDAY, FRIDAYS};
                break;
            case 6:
                week = new String[]{SATURDAY, SATURDAYS};
                break;
        }
        return week;
    }

    private static String getWeekDay(int weekNum) {
        String week = null;
        switch (weekNum) {
            case 0:
                week = SUNDAYS;
                break;
            case 1:
                week = MONDAYS;
                break;
            case 2:
                week = TUESDAYS;
                break;
            case 3:
                week = WEDNESDAYS;
                break;
            case 4:
                week = THURSDAYS;
                break;
            case 5:
                week = FRIDAYS;
                break;
            case 6:
                week = SATURDAYS;
                break;
        }
        return week;
    }

    /**
     * @param dt
     * @return
     * @author ty
     * @createdate 2012-9-28 下午3:42:07
     * @Description: 获得日期的星期数
     */
    public static String getWeekOfDate(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return getWeekDay(w);
    }

    /**
     * @param d   传递进来的Date
     * @param day 第几天
     * @author bo.wang
     * @createdate 2012-10-9 下午4:46:59
     * @Description 获取几天前的Date
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    /**
     * @param day 第几天
     * @author bo.wang
     * @createdate 2012-10-9 下午4:47:14
     * @Description 获取几天后的Date
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * @param dateString 需要转换的字符串
     * @return 返回转换完成的Date，如果转换失败，返回null
     * @author bo.wang
     * @createdate 2012-10-26 下午2:48:04
     * @Description 将string转换成Date，String格式为“2012-10-26”
     */
    public static Date parseStringToDate(String dateString) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateString);
            return date;
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }

        return null;
    }

    /**
     * @return
     * @throws ParseException
     * @author ty
     * @createdate 2013-4-26 下午3:51:48
     * @Description: date转换时间戳
     */
    public static long getTimestamp(Date date1) throws ParseException {
        Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
                .parse("1970/01/01 08:00:00");
        long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime()
                - date2.getTime() : date2.getTime() - date1.getTime();
        long rand = (int) (Math.random() * 1000);
        return rand;
    }

    /**
     * @param dateStr
     * @return
     * @author miaoxin.ye
     * @createdate 2012-10-31 下午7:46:42
     * @Description: 将字符串转化为日期
     */
    public static Date parseStringToDate(String dateStr, DateFormat df) {
        try {
            Date date = df.parse(dateStr);
            return date;
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * @param df   格式化对象
     * @param time 需要转换的字符串
     * @author bo.wang
     * @createdate 2012-10-26 下午3:56:55
     * @Description 将time转换成long型
     */
    public static long parseStringToLong(DateFormat df, String time) {
        try {
            return df.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /**
     * @autor bo.wang
     * @createdate 2012-11-26 下午05:52:57
     * @Description 获取今天后第day天0点时间点
     */
    public static long getTodayZeroTime(int day) {
        java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + day);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);

        ItktLog.d("getTodayZeroTime==>" + f.format(cal.getTime()));

        return cal.getTimeInMillis();
    }

    /**
     * @param day 前多少年
     * @return
     * @author ty
     * @createdate 2013-4-26 下午3:46:12
     * @Description: 获取今天前第N年0点时间点
     */
    public static long getTodayZeroYeay(int day) {
        java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - day);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);

        ItktLog.d("getTodayZeroTime==>" + f.format(cal.getTime()));

        return cal.getTimeInMillis();
    }

    /**
     * @return
     * @author ty
     * @createdate 2012-12-6 下午5:46:54
     * @Description: 获取一年后时间yyyy-MM-dd
     */
    public static String aYearLater() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return TimeUtil.parseDateToString(TimeUtil.sdf1, cal.getTime());
    }

    public static String aYearLater20() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, 1);
        cal.add(Calendar.DAY_OF_MONTH, -20);
        return TimeUtil.parseDateToString(TimeUtil.sdf1, cal.getTime());
    }

    public static String aYearBefor20() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, 1);
        cal.add(Calendar.DAY_OF_MONTH, 20);
        return TimeUtil.parseDateToString(TimeUtil.sdf1, cal.getTime());
    }

    /**
     * @autor bo.wang
     * @createdate 2013-1-8 上午10:21:22
     * @Description 计算历时
     */
    public static String computeTook(int time) {
        StringBuilder builder = new StringBuilder();
        if (time >= 60) { // 大于或等于一个小时
            int hours = time / 60; // 小时数
            builder.append(hours);
            builder.append("小时");
        }

        int minites = time % 60; // 分钟数
        if (minites > 0) {
            builder.append(minites);
            builder.append("分");
        }

        return builder.toString();
    }

    /**
     * @param date
     * @return true 白天 ，false 晚上
     * @author sbk
     * @createdate 2014-5-14 上午10:59:54
     * @Description: 根据当前系统时间判断是白天还是晚上
     */
    public static boolean getDayorNight(Date date) {
        int h = Integer.valueOf(dfHour.format(date).split(":")[0]);
        if (h < 20 || h < 6) {
            return false;
        } else {
            return true;
        }

    }

    public static String getTime() {
        return dfHour.format(new Date());

    }

    /**
     * @param create_time
     * @return
     * @author zhourihu
     * @createdate 2013-12-24 下午2:23:43
     * @Description: (用一句话描述该方法做什么) 将时间戳转换为 模糊时间 其实一般工具类里对异常不做处理，交给调用者处理
     */
    public static String parseTimestampToString(String create_time) {
        long during = 0;
        try {
            during = Long.parseLong(create_time);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return "不久前";
        }
        long nowL = System.currentTimeMillis();// 当前系统毫秒数
        ItktLog.i("当前系统时间戳 毫秒数==" + nowL);
        ItktLog.i("返回的时间戳秒数==" + create_time);
        if (during == 0) {
            return "不久前";
        }
        // during *= 1000;// 传过来的时间戳为妙
        // if(nowL/during>999){//穿过来的为秒
        // during*=1000;
        // }
        during = nowL - during;// 算出距离现在间隔时间 毫秒
        ItktLog.i("距离当前时间毫秒数==" + during);
        during = during / 1000 / 60;// 算出距离现在间隔时间 分钟
        ItktLog.i("距离现在间隔时间 分钟数==" + during);
        if (during <= 0) {// 一分钟前
            return "刚刚";
        } else if (during < 60) {// 由于系统时间差异
            return during + "分钟前";
        } else {
            during = during / 60;// 小时
            if (during < 24) {
                return during + "小时前";
            } else {
                during = during / 24;// 天
                if (during < 30) {
                    return during + "天前";
                } else {
                    during /= 30;// 月
                    if (during < 12) {
                        return during + "月前";
                    } else {
                        during /= 12;// 年
                        return during + "年前";
                    }
                }
            }
        }
    }

    /**
     * @param create_time
     * @return
     * @author zhourihu
     * @createdate 2013-12-26 下午3:07:50
     * @Description: (用一句话描述该方法做什么) 将long 类型的字符串转换为时间
     */
    public static String parseTimestampToDateStr(String create_time) {
        ItktLog.i("返回的时间戳秒数为" + create_time);
        long time = 0;
        Date date = new Date();
        // long current=date.getTime();
        try {
            time = Long.parseLong(create_time);
        } catch (NumberFormatException e) {
            //Auto-generated catch block
            e.printStackTrace();
            return create_time;
        }
        // if(time==0){
        // return "不久前";
        // }
        // if(current/time>1000){//返回为秒
        time *= 1000;// 返回为秒
        // }
        date.setTime(time);
        return parseDateToString(date);
    }

    /**
     * @param time
     * @return
     * @author lgy
     * @createdate 2014-5-28 下午2:00:21
     * @Description: (用一句话描述该方法做什么) 黑龙江新闻时间
     */
    public static String newsDetailTime(String time) {
        if (TextUtil.stringIsNull(time)) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String data = sdf.format(Long.valueOf(time));
        data = data.substring(5, data.length());
        return data;
    }
}
